//添加事件
function loadXMLDoc(){
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp=new XMLHttpRequest();
    } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            var json = JSON.parse(xmlhttp.responseText);
            console.log(json.data);
            for(let i = 0; i<json.data.length; i++){
                var tr = document.createElement("tr");
                var studentName = document.createElement("td");
                studentName.innerHTML = json.data[i].studentName;
                var sex = document.createElement("td");
                sex.innerHTML = json.data[i].sex;
                var age = document.createElement("td");
                age.innerHTML = json.data[i].age;
                tr.appendChild(studentName);
                tr.appendChild(sex);
                tr.appendChild(age);
                document.querySelector("#tb").appendChild(tr);
            }
            //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
    };
    xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/student" ,true);
    xmlhttp.send();
}

loadXMLDoc();


//点击事件
document.querySelector("#btn").addEventListener("click",function(){
    //获取姓名
    var name = document.querySelector("#n").value;
    console.log(name);
    //获取性别
    var sex = document.querySelector("#s").value;
    console.log(sex);
    //获取年龄
    var age = document.querySelector("#a").value;
    console.log(age);



    //ajax 代码
    var list;
    if (window.XMLHttpRequest){
        list=new XMLHttpRequest();
    }
    else {
        list=new ActiveXObject("Microsoft.list");
    }
    list.onreadystatechange=function() {
        if (list.readyState==4 && list.status==200){
            loadXMLDoc();
        }
    };
    list.open("GET","http://localhost/ajaxApi/public/index.php/index/student/addStudent?studentName="+ name +"&sex="+ sex +"&age=" + age ,true);
    list.send();
});


